package com.liuwei.msmall.marketing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuwei.msmall.common.utils.PageUtils;
import com.liuwei.msmall.common.utils.Query;
import com.liuwei.msmall.marketing.dao.SeckillSessionDao;
import com.liuwei.msmall.marketing.entity.SeckillSessionEntity;
import com.liuwei.msmall.marketing.entity.SeckillSkuRelationEntity;
import com.liuwei.msmall.marketing.service.SeckillSessionService;
import com.liuwei.msmall.marketing.service.SeckillSkuRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {


    @Autowired
    SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<SeckillSessionEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            queryWrapper.eq("id",key).or().like("name",key);
        }

        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLatest3DaysSession() {
        //获得开始时间
        String startTime = getStartTime();
        //获得结束时间
        String endTime = getEndTime();

        /**
         * SELECT * FROM `sms_seckill_session`
         WHERE start_time BETWEEN '2020-10-16 09:00:00' AND '2020-10-19 10:00:00'
         */
        List<SeckillSessionEntity> seckillSessionEntities =this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time",startTime,endTime));
        //如果查出来的场次数据不为空，将封装每个秒杀场次所关联的商品到秒杀场次中
        if(seckillSessionEntities!=null&&seckillSessionEntities.size()>0){
            List<SeckillSessionEntity> collect = seckillSessionEntities.stream().map(seckillSessionEntity -> {
                //根据活动场次的Id查出关联的商品信息
                Long id = seckillSessionEntity.getId();
                List<SeckillSkuRelationEntity> seckillSkuRelationEntities = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", id));
                seckillSessionEntity.setSkuRelationEntities(seckillSkuRelationEntities);
                return seckillSessionEntity;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    /**
     * 删除场次信息，以及关联的商品信息
     * @param longs
     */
    @Transactional
    @Override
    public void removeDetail(List<Long> longs) {
        this.removeByIds(longs);
        seckillSkuRelationService.remove(new QueryWrapper<SeckillSkuRelationEntity>().in("promotion_session_id",longs));
    }

    private String getStartTime() {
        LocalTime min = LocalTime.MIN;
        LocalDate now = LocalDate.now();
        LocalDateTime start = LocalDateTime.of(now, min);
        //将日期格式化一下
        return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    private String getEndTime() {
        LocalTime max = LocalTime.MAX;
        LocalDate now = LocalDate.now();
        LocalDate puls2 = now.plusDays(2);
        LocalDateTime end = LocalDateTime.of(puls2, max);
        //将日期格式化一下
        return end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

}